package com.winning.sx.microframework.common.config;

import com.winning.sx.microframework.common.shiro.*;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.shiro.mgt.SecurityManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class ShiroConfig {
    //将自己的验证方式加入容器
    @Bean(name = "WinningShiroRealm")
    public WinningShiroRealm myShiroRealm() {
        WinningShiroRealm winingShiroRealm = new WinningShiroRealm();
        return winingShiroRealm;
    }

    @Bean(name = "CustomShiroRealm")
    public CustomShiroRealm customShiroRealm() {
        CustomShiroRealm realm = new CustomShiroRealm();
        realm.setCredentialsMatcher(new CustomCredentialsMatcher());
        return realm;
    }

    @Bean(name = "DoctorShiroRealm")
    public DoctorShiroRealm doctorShiroRealm() {
        DoctorShiroRealm realm = new DoctorShiroRealm();
        realm.setCredentialsMatcher(new CustomCredentialsMatcher());
        return realm;
    }

    //权限管理，配置主要是Realm的管理认证
//    @Bean
//    public SecurityManager securityManager() {
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setRealm(myShiroRealm());
//        return securityManager;
//    }
    @Bean(name = "SecurityManager")
    public SecurityManager securityManager() {

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //设置realm.
        securityManager.setAuthenticator(modularRealmAuthenticator());
        List<Realm> realms = new ArrayList<>();
        //添加多个Realm
        realms.add(myShiroRealm());
        realms.add(customShiroRealm());
        realms.add(doctorShiroRealm());
        securityManager.setRealms(realms);

        return securityManager;
    }


    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String, String> map = new HashMap<String, String>();

        //webAPI跳过shiro
        map.put("/webAPI/**", "anon");
        //登出
        map.put("/logout", "logout");
        //不进行权限认证
        map.put("/static/**","anon");
//        map.put("/src/static/**", "anon");
        map.put("/assets/**", "anon");
        map.put("/pay/**", "anon");
//        map.put("/src/html/**","anon");//奇怪，必须加src才能行，否则404
        map.put("/login/**", "anon");
        map.put("/admin/**", "anon");
        map.put("/doctor/**", "anon");
        //对所有用户认证
        map.put("/**", "authc");

        map.put("/**.txt","anon");
        map.put("/**.ico","anon");
//        map.put("/**","anon");
        //登录
        shiroFilterFactoryBean.setLoginUrl("/login");
        //首页
        shiroFilterFactoryBean.setSuccessUrl("/index");
        //错误页面，认证不通过跳转
        shiroFilterFactoryBean.setUnauthorizedUrl("/login");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }


    //加入注解的使用，不加入这个注解不生效
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    /**
     * 系统自带的Realm管理，主要针对多realm
     */
    @Bean
    public ModularRealmAuthenticator modularRealmAuthenticator() {
        //自己重写的ModularRealmAuthenticator
        UserModularRealmAuthenticator modularRealmAuthenticator = new UserModularRealmAuthenticator();
        modularRealmAuthenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
        return modularRealmAuthenticator;

    }
}